package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import com.tool.tcp.Sprite;
import org.come.Frame.PartnerArenaWarJframe;
import org.come.bean.OneArenaBean;
import org.come.bean.OneArenaNotes;
import org.come.bean.OneArenaRole;
import org.come.until.AnalysisString;
import org.come.until.FormsManagement;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.PartnerArenaBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcp.NewPart;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

/**
 * @author HGC<br>
 * @time 2020年3月19日 下午2:57:49<br>
 * @class 类名:PartnerArenaMainPanel 伙伴竞技主面板 编号:5<br>
 */
public class PartnerArenaMainPanel extends JPanel {

    private JScrollPane scrollPane;
    private JList<PartnerArenaModelPanel> partnerUnitJpanels;
    private JLabel labRank, labNum;
    private PartnerArenaBtn btnAdd, btnWar, btnExchange, btnRule, btnGet, btnrefinery;
    private JLabel[] luckyDrawImg; // 抽奖区12个格子
    public PartnerArenaMainPanel() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            setPreferredSize(new Dimension(531, 375));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 5);
            offBtn.setBounds(531 - 37, 10, 25, 25);
            this.add(offBtn);

            getScrollPane();
            getLabNum();
            getBtnAdd();
            getBtnWar();
            getLabRank();
            getBtnGet();
            getBtnrefinery();
            getBtnExchange();
        }else {
            setPreferredSize(new Dimension(531, 375));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/s74.png", 1, 5);
            offBtn.setBounds(500, 5, 25, 25);
            this.add(offBtn);

            getScrollPane();
            getLabNum();
            getBtnAdd();
            getBtnWar();
            getLabRank();
            getBtnGet();
            getBtnrefinery();
            getBtnExchange();
        }
    }

    /** 刷新面板 */
    public void showRankView(List<OneArenaRole> list, int place) {
        if (list != null) {
            int size = list.size();
            int componentCount = partnerUnitJpanels.getComponentCount();
            int type = place == 0 ? 0 : (place <= 10 ? 1 : -1);
            for (int i = 0; i < size; i++) {
                if (componentCount < size) {
                    PartnerArenaModelPanel panel = new PartnerArenaModelPanel();
                    panel.setBounds(0, 50 * componentCount, 220, 49);
                    partnerUnitJpanels.add(panel);
                    componentCount++;
                } else if (componentCount > size) {
                    partnerUnitJpanels.remove(componentCount);
                    componentCount--;
                }
                PartnerArenaModelPanel component2 = (PartnerArenaModelPanel) partnerUnitJpanels.getComponent(i);
                component2.showView(list.get(i), type);
            }
            partnerUnitJpanels.setPreferredSize(new Dimension(220, 50 * size));
            refreshWarNum();
            if (place != 0) {
                labRank.setText(String.valueOf(place));
            }
        }
    }

    /** 刷新挑战次数 */
    public void refreshWarNum() {
        // 获取当前剩余挑战次数
        int sumReceive = TaskRoleData.SumReceive(3, 4);
        labNum.setText(String.valueOf(sumReceive));
    }

    /** 展示面板 */
    public void showView(OneArenaBean arenaBean) {
        if (arenaBean == null)
            return;
        List<OneArenaRole> arenaList = arenaBean.getArenaList();
        if (arenaList != null) {
            showRankView(arenaBean.getArenaList(), arenaBean.getPlace());
            if (!FormsManagement.getframe(5).isVisible()) {
                FormsManagement.showForm(5);
            }
        }
        List<OneArenaNotes> notesList = arenaBean.getNotesList();
        if (notesList != null) {
            PartnerArenaWarJframe.getPartnerArenaWarJframe().getPartnerArenaWarPanel().showView(notesList);
            if (!FormsManagement.getframe(80).isVisible()) {
                FormsManagement.showForm(80);
            }
        }
        OneArenaNotes notes = arenaBean.getNotes();
        if (notes != null) {
            if (FormsManagement.getInternalForm2(80) != null) {
                if (FormsManagement.getframe(80).isVisible()) {
                    PartnerArenaWarJframe.getPartnerArenaWarJframe().getPartnerArenaWarPanel().refreshOneNoets(notes);
                }
            }
        }
    }

    private ImageIcon icon;
    static Sprite tcp = SpriteFactory.VloadSprite("resource/mouse/tx/tx14.tcp", null);
    //static Sprite tcp1 = SpriteFactory.VloadSprite("resource/mouse/tx/tx128.tcp", null);
    private NewPart part;
    private int size1, size2, lvlNum;
    private Color turnColor;
    private String name, lvl;
    /** 画名字和等级 */
    public void drawNameAndLvl(Graphics g, int x, int y) {
        if (!ImageMixDeal.userimg.getName().equals(name)) {
            name = ImageMixDeal.userimg.getName();
            size1 = g.getFontMetrics().stringWidth(name) / 2;
        }
        if (lvlNum != ImageMixDeal.userimg.getRoleShow().getGrade()) {
            lvlNum = ImageMixDeal.userimg.getRoleShow().getGrade();
            turnColor = UIUtils.getcolor(ImageMixDeal.userimg.getRoleShow().getTurnAround());
            lvl = AnalysisString.lvl(lvlNum) + "级";
            size2 = g.getFontMetrics().stringWidth(lvl) / 2;
        }
        g.setFont(UIUtils.TEXT_FONT22);
        g.setColor(turnColor);
        g.drawString(name, x -10 - size1, y);
        g.drawString(lvl, x - size2, y+18);

    }
    //tx15 tx20 tx14 tx128  tx12
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (icon == null) {
                icon = new ImageIcon("inkImg/background/S179.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 531, 375, null);
            // 绘制人物动态图
            if (ImageMixDeal.userimg != null) {
                if (part == null) {
                    part = SpriteFactory.createPart(ImageMixDeal.userimg.getRoleShow().getSpecies_id().toString(), 2, 1,
                            null);
                } else {
                    part = SpriteFactory.setPart(part, 1, ImageMixDeal.userimg.getRoleShow().getSpecies_id().toString());
                }
                part.draw(g, 155, 265, 4, ImageMixDeal.userimg.getTime());
                drawNameAndLvl(g, 138, 100);
            }
        }else {
            if (icon == null) {
                icon = new ImageIcon("img/xy2uiimg/S179.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 531, 375, null);
            // 绘制人物动态图
            if (ImageMixDeal.userimg != null) {
                if (part == null) {
                    part = SpriteFactory.createPart(ImageMixDeal.userimg.getRoleShow().getSpecies_id().toString(), 2, 1,
                            null);
                } else {
                    part = SpriteFactory.setPart(part, 1, ImageMixDeal.userimg.getRoleShow().getSpecies_id().toString());
                }
                part.draw(g, 135, 300, 4, ImageMixDeal.userimg.getTime());
                tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
                tcp.draw(g,135,300);
                // tcp1.updateToTime(ImageMixDeal.userimg.getTime(), 0);
                //tcp1.draw(g,135,300);
                drawNameAndLvl(g, 105, 90);
            }
        }
    }


    public JScrollPane getScrollPane() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (scrollPane == null) {
                // 属性滚动条
                scrollPane = new JScrollPane();
                scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.getViewport().setView(getPartnerUnitJpanels());
                scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
                scrollPane.getVerticalScrollBar().setUnitIncrement(20);
                scrollPane.getViewport().setOpaque(false);
                scrollPane.setOpaque(false);
                scrollPane.setBounds(274, 44, 233 + 3, 306);
                scrollPane.setBorder(BorderFactory.createEmptyBorder());
                scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                this.add(scrollPane);
            }
        }else {
            if (scrollPane == null) {
                // 属性滚动条
                scrollPane = new JScrollPane();
                scrollPane.setVerticalScrollBarPolicy(22);
                scrollPane.getViewport().setView(getPartnerUnitJpanels());
                scrollPane.getVerticalScrollBar().setUI(new ScrollUI());
                scrollPane.getVerticalScrollBar().setUnitIncrement(20);
                scrollPane.getViewport().setOpaque(false);
                scrollPane.setOpaque(false);
                scrollPane.setBounds(264, 60, 233 + 3, 280);
                scrollPane.setBorder(BorderFactory.createEmptyBorder());
                scrollPane.setHorizontalScrollBarPolicy(31);
                this.add(scrollPane);
            }
        }
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public JList<PartnerArenaModelPanel> getPartnerUnitJpanels() {
        if (partnerUnitJpanels == null) {
            partnerUnitJpanels = new JList<PartnerArenaModelPanel>();
            partnerUnitJpanels.setSelectionBackground(new Color(122, 117, 112));
            partnerUnitJpanels.setSelectionForeground(Color.white);
            partnerUnitJpanels.setForeground(Color.white);
            partnerUnitJpanels.setFont(UIUtils.TEXT_HY16);
            partnerUnitJpanels.removeAll();
            partnerUnitJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
            // DefaultListCellRenderer cellRenderer = new
            // DefaultListCellRenderer() {
            // @Override
            // public Component
            // getListCellRendererComponent(javax.swing.JList<?> list, Object
            // value, int index,
            // boolean isSelected, boolean cellHasFocus) {
            // super.getListCellRendererComponent(list, value, index,
            // isSelected, cellHasFocus);
            // return this;
            // }
            // };
            // partnerUnitJpanels.setCellRenderer(cellRenderer);
            partnerUnitJpanels.setOpaque(false);
        }
        return partnerUnitJpanels;
    }

    public void setPartnerUnitJpanels(JList<PartnerArenaModelPanel> partnerUnitJpanels) {
        this.partnerUnitJpanels = partnerUnitJpanels;
    }

    public JLabel getLabRank() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (labRank == null) {
                labRank = new JLabel();
                labRank.setBounds(170, 46, 80, 15);
                labRank.setForeground(Color.white);
                labRank.setFont(UIUtils.TEXT_FONT3);
                this.add(labRank);
            }
        }else {
            if (labRank == null) {
                labRank = new JLabel();
                labRank.setBounds(110, 59, 80, 15);//当前排名
                labRank.setForeground(Color.white);
                labRank.setFont(UIUtils.TEXT_FONT22);
                this.add(labRank);
            }
        }
        return labRank;
    }

    public void setLabRank(JLabel labRank) {
        this.labRank = labRank;
    }

    public JLabel getLabNum() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (labNum == null) {
                labNum = new JLabel();
                labNum.setHorizontalAlignment(SwingConstants.CENTER);
                labNum.setBounds(195, 289, 33, 15);
                labNum.setForeground(Color.red);
                labNum.setFont(UIUtils.TEXT_FONT1);
                this.add(labNum);
            }
        }else {
            if (labNum == null) {
                labNum = new JLabel();
                labNum.setHorizontalAlignment(SwingConstants.CENTER);//剩下次数
                labNum.setBounds(122, 113, 33, 15);
                labNum.setForeground(Color.red);
                labNum.setFont(UIUtils.TEXT_FONT1);
                this.add(labNum);
            }
        }
        return labNum;
    }

    public void setLabNum(JLabel labNum) {
        this.labNum = labNum;
    }

    public PartnerArenaBtn getBtnAdd() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (btnAdd == null) {
                btnAdd = new PartnerArenaBtn("inkImg/button/B35.png", 1, 0, this);
                btnAdd.setBounds(231, 289, 17, 17);
                this.add(btnAdd);
            }
        }else {
            if (btnAdd == null) {//加号
                btnAdd = new PartnerArenaBtn("img/xy2uiimg/55_png.button.btn_12.png", 1, 0, this);
                btnAdd.setBounds(148, 112, 17, 17);
                this.add(btnAdd);
            }
        }
        return btnAdd;
    }

    public void setBtnAdd(PartnerArenaBtn btnAdd) {
        this.btnAdd = btnAdd;
    }

    public PartnerArenaBtn getBtnWar() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (btnWar == null) {
                btnWar = new PartnerArenaBtn("inkImg/button/2.png", 1, UIUtils.COLOR_BTNTEXT, "战报", UIUtils.TEXT_FONT, 1,
                        this);
                btnWar.setBounds(84, 312, 34, 17);
                this.add(btnWar);
            }
        }else {
            if (btnWar == null) {
                btnWar = new PartnerArenaBtn("inkImg/hongmu/21_png.button.tab_nex.png", 1, UIUtils.COLOR_BTNTEXT, "战报", UIUtils.TEXT_FONT, 1,
                        this);
                btnWar.setBounds(59, 322, 34, 17);
                this.add(btnWar);
            }
        }
        return btnWar;
    }

    public void setBtnWar(PartnerArenaBtn btnWar) {
        this.btnWar = btnWar;
    }

    public PartnerArenaBtn getBtnExchange() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (btnExchange == null) {
                btnExchange = new PartnerArenaBtn("inkImg/button/2.png", 1, UIUtils.COLOR_BTNTEXT, "兑换", UIUtils.TEXT_FONT,
                        2, this);
                btnExchange.setBounds(124, 312, 34, 17);
                this.add(btnExchange);
            }
        }else {
            if (btnExchange == null) {
                btnExchange = new PartnerArenaBtn("inkImg/hongmu/21_png.button.tab_nex.png", 1, UIUtils.COLOR_BTNTEXT, "兑换", UIUtils.TEXT_FONT,
                        2, this);
                btnExchange.setBounds(99, 322, 34, 17);
                this.add(btnExchange);
            }
        }
        return btnExchange;
    }

    public void setBtnExchange(PartnerArenaBtn btnExchange) {
        this.btnExchange = btnExchange;
    }

    public PartnerArenaBtn getBtnRule() {
        return btnRule;
    }

    public void setBtnRule(PartnerArenaBtn btnRule) {
        this.btnRule = btnRule;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public PartnerArenaBtn getBtnGet() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (btnGet == null) {
                btnGet = new PartnerArenaBtn("inkImg/button/2.png", 1, UIUtils.COLOR_BTNTEXT, "领取", UIUtils.TEXT_FONT, 5,
                        this);
                btnGet.setBounds(164, 312, 34, 17);
                this.add(btnGet);
            }
        }else {
            if (btnGet == null) {
                btnGet = new PartnerArenaBtn("inkImg/hongmu/21_png.button.tab_nex.png", 1, UIUtils.COLOR_BTNTEXT, "领取", UIUtils.TEXT_FONT, 5,
                        this);
                btnGet.setBounds(139, 322, 34, 17);
                this.add(btnGet);
            }
        }
        return btnGet;
    }

    public void setBtnGet(PartnerArenaBtn btnGet) {
        this.btnGet = btnGet;
    }

    public PartnerArenaBtn getBtnrefinery() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (btnrefinery == null) {
                btnrefinery = new PartnerArenaBtn("inkImg/button/2.png", 1, UIUtils.COLOR_BTNTEXT, "炼化", UIUtils.TEXT_FONT,
                        6, this);
                btnrefinery.setBounds(204, 312, 34, 17);
                this.add(btnrefinery);
            }
        }else {
            if (btnrefinery == null) {
                btnrefinery = new PartnerArenaBtn("inkImg/hongmu/21_png.button.tab_nex.png", 1, UIUtils.COLOR_BTNTEXT, "炼化", UIUtils.TEXT_FONT,
                        6, this);
                btnrefinery.setBounds(179, 322, 34, 17);
                this.add(btnrefinery);
            }
        }
        return btnrefinery;
    }

    public void setBtnrefinery(PartnerArenaBtn btnrefinery) {
        this.btnrefinery = btnrefinery;
    }

}
